//
//  TreeViewController.m
//  商城
//
//  Created by runone02 on 2018/9/19.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "TreeViewController.h"
#import "TreeViewNode.h"
#import "Cell.h"

@interface TreeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@end

@implementation TreeViewController{
    //数据源
    NSMutableArray * _dataSource;
    
    //显示的数组
    NSMutableArray * _displayArray;
}

- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(expandCollapseNode:) name:@"TreeNodeClick" object:nil];
    
    [self loadData];
    [self loadDisplayArray];
}

//通知方法
- (void)expandCollapseNode:(NSNotification *)notice{
    [self loadDisplayArray];
    [self.tableView reloadData];
}

/**
 *  下载数据源
 */
- (void)loadData{
    
    if (_dataSource != nil) {
        [_dataSource removeAllObjects];
    }
    _dataSource = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 4; i++) {
        TreeViewNode * treeNode = [[TreeViewNode alloc] init];
        treeNode.nodeLevel = 0;
        treeNode.isExpanded = NO;
        treeNode.nodeName = [NSString stringWithFormat:@"第一级节点 %d", i];
        //        treeNode.nodeChildren = (NSMutableArray *)[self loadSecondDataWithPar:i];
        [_dataSource addObject:treeNode];
    }
}

/**
 *  加载显示的数组
 */
- (void)loadDisplayArray{
    if (_displayArray != nil) {
        [_displayArray removeAllObjects];
    }
    _displayArray = [[NSMutableArray alloc] init];
    for (TreeViewNode * node in _dataSource) {
        [_displayArray addObject:node];
        if (node.isExpanded) {
            //展开状态
            [self loadNodeChirdrenArray:node.nodeChildren];
        }
    }
}

- (void)loadNodeChirdrenArray:(NSArray *)childrenArray{
    for (TreeViewNode *node in childrenArray) {
        [_displayArray addObject:node];
        if (node.isExpanded) {
            [self loadNodeChirdrenArray:node.nodeChildren];
        }
    }
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _displayArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Cell * cell = [tableView dequeueReusableCellWithIdentifier:@"acell"];
    
    if (cell == nil) {
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"acell"];
    }
    TreeViewNode * node = _displayArray[indexPath.row];
    cell.node = node;
    if (cell.node.isExpanded) {
        [cell.button setTitle:@"收起" forState:UIControlStateNormal];
    }else {
        [cell.button setTitle:@"下拉" forState:UIControlStateNormal];
    }
    
    [cell setNeedsDisplay];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TreeViewNode * node = _displayArray[indexPath.row];
    node.isExpanded = !node.isExpanded;
    //循环获取下级，直至为空
    [self loadDataWithPar:node];
}

- (void) loadDataWithPar:(TreeViewNode *)parNode{
    //模拟从网络获取数据
    NSMutableArray * array = [NSMutableArray new];
    if (parNode.nodeLevel < 3) {
        for (int j = 0; j < 4; j++) {
            TreeViewNode * treeNode = [[TreeViewNode alloc] init];
            treeNode.nodeLevel = parNode.nodeLevel + 1;
            treeNode.isExpanded = NO;// 默认是NO
            treeNode.nodeName = [NSString stringWithFormat:@"等级 %lu 的下一级节点 %d", (unsigned long)treeNode.nodeLevel - 1, j];
            [array addObject:treeNode];
        }
        parNode.nodeChildren = array;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TreeNodeClick" object:nil];
        
    }else {
        NSLog(@"%@", parNode);
    }
    
}


@end
